<?php
/**  
  *
  * Copyright (c) 2009, Persistent Systems Limited
  *
  * Redistribution and use, with or without modification, are permitted 
  *  provided that the following  conditions are met:
  *   - Redistributions of source code must retain the above copyright notice, 
  *     this list of conditions and the following disclaimer.
  *   - Neither the name of Persistent Systems Limited nor the names of its contributors 
  *     may be used to endorse or promote products derived from this software 
  *     without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
/**
  *
  * This file contains the definition of ADODotNETDataServicesEditor class.
  */ 

require_once 'ADODotNETDataServicesEntity.php';

/**
  * The ADODotNETDataServicesEditor Class.
  *
  * @copyright  2009 Persistent Systems Limited
  */

class ADODotNETDataServicesEditor
{
    protected $_containerName;
    protected $_containerScriptName;
    protected $_http_proxy_host;
    protected $_http_proxy_port;
    protected $_userName;
    protected $_password;

    /**
     * Constructs a new ADODotNETDataServicesEditor object.
     * @param string $containerScriptName The Container PHP Script Name.
     * @param string $containerName The Container class Name.
     * @param string $serviceUri ADO .NET Data Service URI.
     * @param string $http_proxy_host HTTP Proxy host name.
     * @param string $http_proxy_port HTTP Proxy port number.
     * @param string $userName user name for authenticating ADO .NET Data Service.
     * @param string $password password for authenticating ADO .NET Data Service.
     */
    public function ADODotNETDataServicesEditor($containerScriptName, $containerName, $serviceUri = "", 
        $http_proxy_host = "", $http_proxy_port = "", $userName = "", $password = "")
    {
        $this->_containerScriptName = $containerScriptName;
        $this->_containerName = $containerName;
        $this->_serviceUri = $serviceUri;
        $this->_proxy = $http_proxy_host;
        $this->_port = $http_proxy_port;
        $this->_userName = $userName;
        $this->_password = $password;
    }

    /**
     * This function displays the editor
     */
    public function show()
    {
?>
<html>
    <head>
    </head>
    <body>
        <table style="border: thin solid #C0C0C0; width:100%" align="left" >
            <form action="<?php  echo $this->_containerScriptName; ?>" method="post">
                <tr>
                     <td style="font-family: calibri; font-size: medium;">
                        Data Service : <?php  echo $this->_serviceUri; ?>
                        <input type="hidden" name="txtUri" value="<?php  echo $this->_serviceUri; ?>" />
                    </td>
                </tr>
                <tr>
                    <td style="font-family: calibri; font-size: medium;">
                        Query &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:
                        <input style="width:550pt;" id="txtQuery" type="text" name="txtQuery" value="<?php if(isset($_POST['txtQuery']) == 1) echo $_POST['txtQuery']; elseif(isset($_GET['query']) == 1) echo $_GET['query'];?>"/>
                        <input id="btnResult" type="submit" name="btnResult" value="Submit Query" />
                    </td>
                </tr>
            </form>
            <tr>
                <td colspan="3" style="width: 100%;">
                    <div>
                    <?php
                        $serviceUri = '';
                        $query = '';

                        if (isset($_GET['serviceUri']))
                        {
                            $serviceUri = $_GET['serviceUri'];
                        }
                        
                        if (isset($_GET['query']))
                        {
                            $query = $_GET['query'];
                        }

                        if (isset($_POST['btnResult']) == 1) 
                        {
                            $serviceUri = $_POST['txtUri'];
                            $query = $_POST['txtQuery'];
                        }

                        if ($serviceUri != '' || $query != '') 
                        {
                            $getResult = new ADODotNETDataServicesEntity($this->_containerScriptName, $this->_containerName, $serviceUri, $query, 
                                             $this->_proxy, $this->_port, $this->_userName, $this->_password);
                            if (isset($_POST['btnDelete'])) 
                            {
                                $getResult->Delete($_POST, $_GET['Type']);
                            } 
                            else if (isset($_POST['btnUpdate'])) 
                            {
                                $getResult->Update($_POST, $_GET['Type']);
                            } 
                            else if (isset($_POST['btnSave'])) 
                            {
                                $getResult->Insert($_POST, $_GET['Type']);
                            } 
                            else if (isset($_POST['btnAddLink'])) 
                            {
                                $getResult->AddLink($_POST, $_GET['Type'], $_GET['AttachQuery']);
                            }
                            $getResult->display();

                            if (isset($_POST['btnDetail'])) 
                            {
                                if (isset($_GET['changeId'])) 
                                {
                                    $getResult->displayDetails($_GET['changeId'], $_GET['Type']);
                                }
                            } 
                            else if (isset($_POST['btnAdd'])) 
                            {
                                $getResult->displayAdd($_GET['Type']);
                            }
                            else if (isset($_GET['AttachTo'])) 
                            {
                                $getResult->displayAddLink($_GET['Type'], $_GET['AttachTo']);
                            }
                        }
                        else 
                        {
                            $getResult = new ADODotNETDataServicesEntity($this->_containerScriptName, $this->_containerName, $this->_serviceUri, '', 
                                             $this->_proxy, $this->_port, $this->_userName, $this->_password);
                            $getResult->display();
                        }
                        ?>
                    </div>
                </td>
            </tr>
        </table>
    </body>
</html>
<?php
    }
}
?>
